// 引入重置样式
require("../../assets/css/reset.css");
// 引入公共样式
require("../../assets/css/basic.css");
// 引入矢量图
require("../../assets/fonts/iconfont.css");
// 引入当前样式
require("./login.less");

// 引入dom
const dom = require("../../utils/dom.js");
// 引入axios模块
const axios = require("axios");

window.addEventListener("DOMContentLoaded", () => {
	// 表单验证
	dom.get("#btn").addEventListener("click", async function () {
		const error = dom.get("#error"); //提示文字
		// 账号验证
		const tel = dom.get("#tel").value;
		let regex = /^1[3456789]\d{9}$/i;
		if (regex.test(tel)) {
			error.textContent = "";
		} else {
			error.textContent = "账号输入错误";
			return;
		}

		// 密码验证
		const pwd = dom.get("#pwd").value;
		if (pwd.length >= 6) {
			error.textContent = "";
		} else {
			error.textContent = "账号密码错误";
			return;
		}

		// 验证成功
		let res = await axios.post(
			"http://fitness.h5.itsource.cn:3701/api/user/login",
			{
				account: tel,
				password: pwd,
			}
		);

		if (res.data.errno === 0) {
			//登录成功就存储token
			localStorage.setItem("token", res.data.data.token);
			alert("登录成功");
			location.href = "index.html";
		} else {
			alert(res.data.message);
		}
	});
});
